package com.ityj.cmn.controller;

import com.ityj.cmn.service.DictService;
import com.ityj.yygh.model.cmn.Dict;
import com.ityj.yygh.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典")
@RestController
@RequestMapping(path = "/admin/cmn/dict")
@Slf4j
public class DictController {

    @Autowired
    private DictService dictService;

    // 根据数据ID查询子数据列表
    @GetMapping(path = "/getChildDataById/{id}")
    public Result<List<Dict>> getChildDataById(@PathVariable Long id) {
        log.info("Get into getChildDataById()...");
        List<Dict> childData = dictService.getChildDataById(id);
        return Result.ok(childData);
    }

    // 根据parent_id 和 value查询出对应的name
    @GetMapping(path = "/getDictName/{value}/{dictCode}")
    public String getDictName(@PathVariable String value, @PathVariable String dictCode) {

        if (StringUtils.isAnyEmpty(value, dictCode)) {
            log.error("Please check input parameters!");
            return "";
        }

        return dictService.getDictName(value, dictCode);
    }

    // 查省市区时只需要value即可
    @GetMapping(path = "/getDictName/{value}")
    public String getDictName(@PathVariable String value) {

        if (StringUtils.isAnyEmpty(value)) {
            log.error("Please check input parameters!");
            return "";
        }

        return dictService.getDictName(value, "");
    }

    // 根据dictCode查询出对应的Dict列表
    @GetMapping(path = "/getDictByDictCode/{dictCode}")
    public Result<List<Dict>> getDictByDictCode(@PathVariable String dictCode) {

        if (StringUtils.isAnyEmpty(dictCode)) {
            log.error("Please check input parameters!");
            return Result.fail();
        }

        List<Dict> dictList = dictService.getDictByDictCode(dictCode);
        return Result.ok(dictList);
    }
}
